/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

/**
 *
 * @author kaszubm9
 */
public class RealTime {

    public static void checkInfo(String startArea, String endArea,
			String startStop, String endStop, Date date, int time){
        Random randomGenerator = new Random();
        int randomInt = randomGenerator.nextInt(100);
        if(randomInt<50){
        delayTest(startArea, endArea, startStop, endStop, date, time, false, randomInt);}
        else{
        delayTest(startArea, endArea, startStop, endStop, date, time, true, randomInt);
        }


    

}
      


      public static void delayTest(String startArea, String endArea,
			String startStop, String endStop, Date date, int time, boolean dalay, int random)
   	{
		boolean change = false;
		ArrayList<Journey> possChecks4 = PassengerChoice.findJourneyOptions(
				startArea, endArea, startStop, endStop, date);
		if (possChecks4.isEmpty())
		{
			ArrayList<Journey> firstCheck = PassengerChoice.findChangeOptionsFirst(
					startArea, endArea, startStop, endStop, date);
			if (firstCheck.isEmpty())
				Timetables.printNoJourney();
			else
			{
                PassengerChoice first;
                if(dalay)
                    first = new PassengerChoice(time+random*4, firstCheck);
                else
                    first = new PassengerChoice(time, firstCheck);
				Journey firstOptimal = first.getOptimal();
				ArrayList<Journey> secondCheck = PassengerChoice.findJourneyOptions(
						"Marple", endArea, "Navigation Hotel", endStop, date);
				PassengerChoice second = new PassengerChoice(firstOptimal
						.getArrivalTime(), secondCheck);
				Journey secondOptimal = second.getOptimal();
				if (!secondCheck.isEmpty())
					change = true;
                printDalay(firstOptimal, secondOptimal, dalay);

			}
		}
		if (!change)
		{
			if (possChecks4.isEmpty())
                Timetables.printNoJourney();
			else
			{
                PassengerChoice example4;
                if(dalay)
                    example4 = new PassengerChoice(time+random*2, possChecks4);
                else
                    example4 = new PassengerChoice(time, possChecks4);
				Journey optimalJourney4 = example4.getOptimal();
				printDalay(optimalJourney4,dalay);
			}
		}
	}

        static void printDalay(Journey firstOptimal, Journey secondOptimal, boolean dalay) {
        try{
             // Create file
            String routeName=BusStopInfo.getRouteName(firstOptimal.getRoute());
            FileWriter fstream = new FileWriter("RealTime.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write("LINE: "+routeName);
            out.newLine();
            out.newLine();
            if(!dalay)
                out.write("The service is running acording to plan");
            else{
                out.write("The service is delayed due to a shortage of water for the windscreen wipers");
                out.newLine();
                out.write("We apologize for the delay to your journey.");
                out.newLine();
                out.write("Next service is due:");
            }
            int atime=firstOptimal.getDepoTime();
            String timef=Timetables.fill("", DateValidation.getHour(atime) , DateValidation.getMinuts(atime));
            out.newLine();
            out.write("Departure:  "+timef +"   "+BusStopInfo.getFullName(firstOptimal.getStartStop()));
            out.newLine();
            out.newLine();
            int dtime=firstOptimal.getArrivalTime();
            String timed=Timetables.fill("", DateValidation.getHour(dtime) , DateValidation.getMinuts(dtime));
            out.write("Arrrival:  "+timed +"   "+BusStopInfo.getFullName(firstOptimal.getEndStop()));
            out.newLine();
            out.newLine();
            out.write("Number of middle bus stops:  "+firstOptimal.getNoOfBusStopsBetween());
            out.newLine();
            out.newLine();
            String routeName2=BusStopInfo.getRouteName(secondOptimal.getRoute());
            out.write("You need to chnge bus for LINE: "+ routeName2);
            int atime2=secondOptimal.getDepoTime();
            String timef2=Timetables.fill("", DateValidation.getHour(atime2) , DateValidation.getMinuts(atime2));
            out.newLine();
            out.newLine();
            out.write("Departure:  "+timef2 +"   "+BusStopInfo.getFullName(secondOptimal.getStartStop()));
            out.newLine();
            out.newLine();
            int dtime2=secondOptimal.getArrivalTime();
            String timed2=Timetables.fill("", DateValidation.getHour(dtime2) , DateValidation.getMinuts(dtime2));
            out.write("Arrrival:  "+timed2 +"   "+BusStopInfo.getFullName(secondOptimal.getEndStop()));
            out.newLine();
            out.newLine();
            out.write("Number of middle bus stops:  "+secondOptimal.getNoOfBusStopsBetween());
            out.close();
        }
        catch (IOException e){//Catch exception if any
             System.err.println("Error: " + e.getMessage());
        }

    }

            public static void printDalay(Journey bestRoute, boolean dalay){
        try{
             // Create file
            String routeName=BusStopInfo.getRouteName(bestRoute.getRoute());
            FileWriter fstream = new FileWriter("RealTime.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write("LINE: "+routeName);
            out.newLine();
            out.newLine();
            if(!dalay)
                out.write("The service is running acording to plan");
            else{
                out.write("The service is delayed due to a shortage of water for the windscreen wipers");
                out.newLine();
                out.write("We apologize for the delay to your journey.");
                out.newLine();
                out.write("Next service is due:");
            }
            int atime=bestRoute.getDepoTime();
            String timef=Timetables.fill("", DateValidation.getHour(atime) , DateValidation.getMinuts(atime));
            out.newLine();
            out.write("Departure:  "+timef +"   "+BusStopInfo.getFullName(bestRoute.getStartStop()));
            out.newLine();
            out.newLine();
            int dtime=bestRoute.getArrivalTime();
            String timed=Timetables.fill("", DateValidation.getHour(dtime) , DateValidation.getMinuts(dtime));
            out.write("Arrrival:  "+timed +"   "+BusStopInfo.getFullName(bestRoute.getEndStop()));
            out.newLine();
            out.newLine();
            out.write("Number of middle bus stops:  "+bestRoute.getNoOfBusStopsBetween());
            out.close();
        }
        catch (IOException e){//Catch exception if any
             System.err.println("Error: " + e.getMessage());
        }
    }



}
